package nc.ui.cmp.applaybill.command;

import java.util.List;

import nc.cmp.utils.CheckException;
import nc.cmp.utils.CmpUtils;
import nc.ui.cmp.applaybill.ClientUI;
import nc.ui.cmp.applaybill.common.UIUtil;
import nc.ui.pub.ButtonObject;
import nc.ui.pub.beans.UITable;
import nc.ui.trade.bill.BillTemplateWrapper;
import nc.ui.trade.bocommand.IButtonCommand;
import nc.ui.trade.button.IBillButton;
import nc.ui.trade.controller.IControllerBase;
import nc.ui.trade.manage.BillManageUI;
import nc.ui.trade.manage.ManageEventHandler;
import nc.vo.cmp.applaybill.ApplyBillVO;
import nc.vo.pub.AggregatedValueObject;

public class ReturnBoCommand extends ManageEventHandler implements IButtonCommand{

	public ReturnBoCommand(BillManageUI billUI, IControllerBase control) {
		super(billUI, control);
		// TODO Auto-generated constructor stub
	}

	public void execute(ButtonObject bo) throws Exception {
		ClientUI clientUI = (ClientUI)getBillUI();
		AggregatedValueObject onlyOneBean = UIUtil.getOnlyOneBean(clientUI);
		ApplyBillVO bean = (ApplyBillVO) onlyOneBean.getParentVO();
		clientUI.setCurrentPanel(BillTemplateWrapper.LISTPANEL);
//		if(CheckException.checkContionsIsNull(clientUI.getUiContext().getCurrentBeanList()) || bean.getPrimaryKey() == null) {
//			
//		}else if(!(bean.getPrimaryKey() == null && CheckException.checkContionsIsNull(clientUI.getUiContext().getCurrentBeanList()))) {
//			List<ApplyBillVO> currentBeanList = clientUI.getUiContext().getCurrentBeanList();
//			ApplyBillVO[] billVOs = CmpUtils.covertListToArrays(currentBeanList, ApplyBillVO.class);
//			clientUI.setListHeadData(billVOs);
//			int index = currentBeanList.indexOf(bean);
//			UITable table = clientUI.getBillListPanel().getHeadTable();
//			if(index != -1) {
//				table.getSelectionModel().setSelectionInterval(index, index);
//			}else {
//				table.getSelectionModel().setSelectionInterval(0, 0);
//			}
//			UIUtil.updateButtonsByBean(bean, clientUI);
//		}else {
//			clientUI.getButtonManager().getButton(IBillButton.Refresh).setEnabled(true);
//			clientUI.updateButtons();
//		}
		if(!CheckException.checkContionsIsNull(clientUI.getUiContext().getCurrentBeanList())) {
			List<ApplyBillVO> currentBeanList = clientUI.getUiContext().getCurrentBeanList();
			ApplyBillVO[] billVOs = CmpUtils.covertListToArrays(currentBeanList, ApplyBillVO.class);
			if(bean.getPrimaryKey() == null) {
				bean = billVOs[0];
			}else {
				clientUI.setListHeadData(billVOs);
				int index = currentBeanList.indexOf(bean);
				UITable table = clientUI.getBillListPanel().getHeadTable();
				if(index != -1) {
					table.getSelectionModel().setSelectionInterval(index, index);
				}else {
					table.getSelectionModel().setSelectionInterval(0, 0);
				}
			}
			UIUtil.updateButtonsByBean(bean, clientUI);
		}else {
			clientUI.getButtonManager().getButton(IBillButton.Refresh).setEnabled(true);
			clientUI.updateButtons();
		}
		
	}

}
